package com.hsxc.cdz.base.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author 刘力文 byte数组的操作工具类
 *
 */
public class BytesUtil {

	private  static Logger logger = LoggerFactory.getLogger(BytesUtil.class);
	/**
	 * 拼接两个byte数组
	 * 
	 * @param a
	 *            byte数组
	 * @param b
	 *            byte数组
	 * @return byte数组
	 */
	public static byte[] concat(byte[] a, byte[] b) {
		if (a == null) {
			return b;
		} else if (b == null) {
			return a;
		} else {
			byte[] c = new byte[a.length + b.length];
			System.arraycopy(a, 0, c, 0, a.length);
			System.arraycopy(b, 0, c, a.length, b.length);
			return c;
		}
	}

	/**
	 * 
	 * @param start
	 *            开始位置
	 * @param num
	 *            获取数量
	 * @param data
	 *            源数组
	 * @return byte 数组
	 */
	public static byte[] getByte(int start, int num, byte[] data) {
		if (data == null) {
			return null;
		}
		if (start > data.length || start + num > data.length) {
			return null;
		}
		byte[] result = new byte[num];
		System.arraycopy(data, start, result, 0, num);
		return result;
	}

	// byte数组的转置
	public static byte[] reverseByte(byte[] bytes) {
		if (bytes == null) {
			return null;
		}
		int tempLength = bytes.length;
		byte[] temp = new byte[tempLength];
		for (int i = 0; i < tempLength; i++) {
			temp[i] = bytes[tempLength - 1 - i];
		}
		return temp;
	}

	// 字符串转为Byte数组
	public static byte[] strToBcdBytes(String s, int i) {
		if (s == null) {
			return null;
		}
		byte[] temp = str2Bcd(s);
		int length = temp.length;
		if (i < length) {
			length = i;
		}
		byte[] result = new byte[i];
		System.arraycopy(temp, 0, result, 0, length);
		if (i > length) {
			for (; length < i; length++) {
				result[length] = 0x00;
			}
		}
		return result;
	}

	// 字符串转为bcd码的byte数组
	private static byte[] str2Bcd(String asc) {
		if (asc == null) {
			return null;
		}
		int len = asc.length();
		int mod = len % 2;
		if (mod != 0) {
			asc = asc + "0";
			len = asc.length();
		}
		byte abt[] = new byte[len];
		if (len >= 2) {
			len = len / 2;
		}
		byte bbt[] = new byte[len];
		abt = asc.getBytes();
		int j, k;
		for (int p = 0; p < asc.length() / 2; p++) {
			if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
				j = abt[2 * p] - '0';
			} else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
				j = abt[2 * p] - 'a' + 0x0a;
			} else {
				j = abt[2 * p] - 'A' + 0x0a;
			}
			if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
				k = abt[2 * p + 1] - '0';
			} else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
				k = abt[2 * p + 1] - 'a' + 0x0a;
			} else {
				k = abt[2 * p + 1] - 'A' + 0x0a;
			}
			int a = (j << 4) + k;
			byte b = (byte) a;
			bbt[p] = b;
		}
		return bbt;
	}

	// byte数组转换为字符串
	public static String bcdBytesToStr(byte[] bytes) {
		return bcd2Str(bytes);
	}

	// bcd格式的byte数组转为字符串
	private static String bcd2Str(byte[] bytes) {
		if (bytes == null) {
			return null;
		}
		char temp[] = new char[bytes.length * 2], val;

		for (int i = 0; i < bytes.length; i++) {
			val = (char) (((bytes[i] & 0xf0) >> 4) & 0x0f);
			temp[i * 2] = (char) (val > 9 ? val + 'A' - 10 : val + '0');

			val = (char) (bytes[i] & 0x0f);
			temp[i * 2 + 1] = (char) (val > 9 ? val + 'A' - 10 : val + '0');
		}
		return new String(temp);
	}

	/**
	 * Bin码格式的byte数组转字符串
	 * 
	 * @param bytes
	 *            源数组
	 * @return 转码后字符串
	 */
	public static String binBytesToStr(byte[] bytes) {
		return byteArr2hexStr(bytes);
	}

	public static byte[] strToBinBytes(String s, int i) {
		logger.info("s.length"+s.length());
		logger.info("i"+i);
		if (s == null) {
			return null;
		}
		byte[] temp = hexStr2ByteArr(s);
		int length = temp.length;
		logger.info("temp.length"+length);
		if (i < length) {
			length = i;
		}
		byte[] result = new byte[i];
		System.arraycopy(temp, i - length, result, 0, length);
		if (i > length) {
			for (; length < i; length++) {
				result[i - length - 1] = 0x00;
			}
		}
		return result;
	}

	// bin码的字符串转为十六进制的字符串
	private static String byteArr2hexStr(byte[] arrB) {
		if (arrB == null) {
			return null;
		}
		int iLen = arrB.length;
		StringBuffer sb = new StringBuffer(iLen * 2);
		for (int i = 0; i < iLen; i++) {
			int intTmp = arrB[i];
			while (intTmp < 0) {
				intTmp = intTmp + 256;
			}
			if (intTmp < 16) {
				sb.append("0");
			}
			sb.append(Integer.toString(intTmp, 16));
		}
		return sb.toString().toUpperCase();
	}

	// 字符串转为Bin码
	public static byte[] hexStr2ByteArr(String strIn) {
		if (strIn == null) {
			return null;
		}
		byte[] arrB = strIn.getBytes();
		int iLen = arrB.length;

		byte[] arrOut = new byte[iLen / 2];
		for (int i = 0; i < iLen; i = i + 2) {
			String strTmp = new String(arrB, i, 2);
			arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
		}
		return arrOut;
	}

	// int数转为正序的byte数组
	public static byte[] intToBytesDesc(int value, int n) {
		byte[] src = new byte[n];
		for (int i = 0; i < n; i++) {
			src[n - 1 - i] = (byte) ((value >> (n - 1 - i) * 8) & 0xFF);
		}
		return src;
	}

	// int数组转为倒序的byte数组
	public static byte[] intToBytesSeq(int value, int n) {
		byte[] src = new byte[n];
		for (int i = 0; i < n; i++) {
			src[i] = (byte) ((value >> (n - 1 - i) * 8) & 0xFF);
		}
		return src;
	}

	// 倒序byte数组转为int数
	public static int bytesToIntDesc(byte[] src) {
		int value = 0;
		if (src == null) {
			return 0;
		}
		int n = src.length;
		for (int i = 0; i < n; i++) {
			value = value | ((src[i] & 0xFF) << i * 8);
		}
		return value;
	}

	// 正序byte数组转换成int
	public static int bytesToIntSeq(byte[] src) {
		int value = 0;
		if (src == null) {
			return value;
		}
		int n = src.length;
		for (int i = 0; i < n; i++) {
			value = value | ((src[i] & 0xFF) << (n - 1 - i) * 8);
		}
		return value;
	}

	// long类型的转换成倒序的byte数组
	public static byte[] longToBytesDesc(long value, int n) {
		byte[] src = new byte[n];
		for (int i = 0; i < n; i++) {
			src[n - 1 - i] = (byte) ((value >> (n - 1 - i) * 8) & 0xFF);
		}
		return src;
	}

	// 倒序byte数组的转换成long
	public static long bytesToLongDesc(byte[] src) {
		long value = 0;
		if (src == null) {
			return value;
		}
		int n = src.length;
		for (int i = 0; i < n; i++) {
			value = value | ((src[i] & 0xFF) << i * 8);
		}
		return value;
	}

	// 地址字符串转Byte数组
	public static byte[] adrressTobytes(String address) {
		if (address == null) {
			return null;
		}
		String[] addrs = address.split("\\.");
		System.out.println(addrs[0]);
		byte[] result = null;
		for (int i = 0; i < addrs.length; i++) {
			result = BytesUtil.concat(result, intToBytesSeq(Integer.valueOf(addrs[i]), 1));
		}
		return result;
	}

	// 字符串转arscII
	public static String stringToAscii(String value) {
		StringBuffer sbu = new StringBuffer();
		char[] chars = value.toCharArray();
		for (int i = 0; i < chars.length; i++) {
			if (i != chars.length - 1) {
				sbu.append((int) chars[i]).append(",");
			} else {
				sbu.append((int) chars[i]);
			}
		}
		return sbu.toString();
	}

	// arscII转字符串
	public static String asciiToString(String value) {
		StringBuffer sbu = new StringBuffer();
		String[] chars = value.split(",");
		for (int i = 0; i < chars.length; i++) {
			sbu.append((char) Integer.parseInt(chars[i]));
		}
		return sbu.toString();
	}
}
